using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace Xfinity.AI
{
    public delegate float FitnessEvaluationDelegate<T>(T organism);
    public struct OrganismDescription<T>
	{
        public OrganismDescription(T organism, float fitness)
        {
            Organism = organism;
            Fitness = fitness;
        }
        public T Organism;
        public float Fitness;
	}
    public class GeneticEvolver<T>
    {
        public GeneticEvolver()
        {

        }
        public GeneticEvolver(IEnumerable<T> existingPopulation)
        {
            population.AddRange(existingPopulation);
        }
        private List<T> population = new List<T>();

        public List<T> Population
        {
            get { return population; }
        }
        public List<OrganismDescription<T>> EvolvePopulation(FitnessEvaluationDelegate<T> evaluator, int generations)
        {
            List<OrganismDescription<T>> evaluated = new List<OrganismDescription<T>>();
            for (int i = 0; i < generations; i++)
            {
                foreach (T organism in population)
                {
                    float fitness = evaluator(organism);
                    evaluated.Add(new OrganismDescription<T>(organism, fitness));
                }
                CrossBreed(evaluated);
            }
            return evaluated;
        }

        private void CrossBreed(List<OrganismDescription<T>> evaluated)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
